package com.jeeplus.modules.guide.service;
import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.modules.guide.entity.MobileMatterLabel;
import com.jeeplus.modules.guide.mapper.VersionMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RequiredArgsConstructor
@Slf4j
@Service
@Transactional(readOnly = true)
public class VersionService {
    //版本缓存 系统缓存
    private static final String VERSION = "version";
    //新版本缓存 事项抓取版本缓存
    private static final String NOW_VERSION = "now_version";
    private final VersionMapper versionMapper;
    private final MobileItemService mobileItemService;

    /*
     *获取当前最新版本号
     */
    @Transactional(readOnly = false)
    public String insertNowVersion() {
        HashMap<String, String> map = versionMapper.getVersion();
        if (map != null) {
            //获取当前版本号
            String value = map.get("value");
            String key = value.substring(0, 1);
            int version = Integer.valueOf(value.substring(1, value.length())) + 1;
            //配置版本号
            value = key + version;
            int sort = Integer.valueOf(map.get("sort")) + 1;
            String uuid = UUID.randomUUID().toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            versionMapper.inserVersion(value, map.get("typeId"), String.valueOf(sort), "版本号:" + value, uuid, sdf.format(new Date()));
            CacheUtils.put(NOW_VERSION, value);
            return value;

        } else {
            return null;
        }
    }

    /*
     *从缓存获取当前版本号
     */
    public String getNowCacheVersion() {
        Object value = CacheUtils.get(NOW_VERSION);
        if (value != null) {
            return (String) value;
        } else {
            HashMap<String, String> map = versionMapper.getVersion();
            if (map != null) {
                //获取当前版本号
                return map.get("value");

            }else{
                return null;
            }
        }
    }

    /*
     *获取所有版本号
     */
    public List<HashMap<String, String>> getVersionList() {
        return versionMapper.getVersionList();
    }

    /*
     *从缓存当中获取版本号
     */
    public String getCacheVersion() {
        Object value = CacheUtils.get(VERSION,"version");
        if (value != null) {
            return (String) value;
        } else {
            return null;
        }
    }

    /*
     *更新缓存版本号
     */
    @Transactional(readOnly = false)
    public void updateCacheVersion(String version) {
        mobileItemService.selectMatterLabelLose(version);
        CacheUtils.put(VERSION,"version",version);
    }

    /*
     *获取当前版本号  并加入缓存
     */
    public String getVersion() {
        HashMap<String, String> map = versionMapper.getVersion();
        if (map != null) {
            //获取当前版本号
            String value = map.get("value");
            //加入缓存
            CacheUtils.put(VERSION,"version",value);
            return value;
        }else{
            return getCacheVersion();
        }
//        if (getCacheVersion() == null) {
//            HashMap<String, String> map = versionMapper.getVersion();
//            if (map != null) {
//                //获取当前版本号
//                String value = map.get("value");
//                //加入缓存
//                CacheUtils.put(VERSION,"version",value);
//                return value;
//            }else{
//                return null;
//            }
//        } else {
//            return getCacheVersion();
//        }
    }

    @Transactional(readOnly = false)
    public void updateYmbcsMatter(String oldVersion,String version){
        versionMapper.updateSlefHlep(version,oldVersion);
        versionMapper.updateYmbcsMatter(oldVersion,version);
        versionMapper.updateMatterArea(version);
        versionMapper.updateMatterStreet(version);
    }
  }
